<?php
namespace app\mapi\controller;
use app\mapi\model\MallGoodsModel;
use app\mapi\model\MallClassModel;
use app\mapi\model\MallImageModel;

/**
 * 商品列表控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class Malls extends Base{

    # 首页商品
    public function mallIndex(){
        if(request()->isPost()){
            $Limit = input('post.limit','','intval') ? input('post.limit','','intval') : 3;

            $MallGoods = new MallGoodsModel;

            $Field = 'goods_id,goods_name,goods_img,goods_primary,goods_present';

            $ListJp = $MallGoods->mAll(['goods_refined'=>1], $Field, $Limit);
            $ListXp = $MallGoods->mAll(['goods_new'=>1], $Field, $Limit);
            $ListHr = $MallGoods->mAll(['goods_hot'=>1], $Field, $Limit);

            if(!empty($ListJp) || !empty($ListXp) || !empty($ListHr)){
                $Code = 1;
                $Msgs = '查询成功';

                $ListGoods = [
                    'GoodsJp'   =>  $ListJp,
                    'GoodsXp'   =>  $ListXp,
                    'GoodsHr'   =>  $ListHr
                ];
            }else{
                $Code = 2;
                $Msgs = '暂无商品';
            }

            $this->ShowAjax($Code, $Msgs, $ListGoods);
        }
    }

    # 首页轮播
    public function mallImage(){
        if(request()->isPost()){
            $Limit = input('post.limit','','intval') ? input('post.limit','','intval') : 5;

            $MallImage = new MallImageModel;

            $ListImage = $MallImage->mAll([], 'image_name,image_img,image_url', $Limit);

            if(!empty($ListImage)){
                $Code = 1;
                $Msgs = '查询成功';
            }else{
                $Code = 2;
                $Msgs = '暂无轮播';
            }

            $this->ShowAjax($Code, $Msgs, $ListImage);
        }
    }

    # 商品分类
    public function mallClass(){
        if(request()->isPost()){
            $MallClass = new MallClassModel;

            $ListClass = $MallClass->mAll(['class_pid'=>0], 'class_id,class_name');

            if(!empty($ListClass)){
                $Code = 1;
                $Msgs = '查询成功';
            }else{
                $Code = 2;
                $Msgs = '暂无分类';
            }

            $this->ShowAjax($Code, $Msgs, $ListClass);
        }
    }

    # 商品列表
    public function mallGoods(){
        if(request()->isPost()){
            $Class = input('post.class_id','','intval');
            $Pages = input('post.page','','intval') ? input('post.page','','intval') : 0;
            $Limit = input('post.limit','','intval') ? input('post.limit','','intval') : 10;

            if(empty($Class)){
                $this->ShowAjax(2, '缺少class_id参数');
            }

            $MallGoods = new MallGoodsModel;

            $ListGoods = $MallGoods->mList(['goods_class'=>$Class], 'goods_id,goods_class,goods_name,goods_img,goods_primary,goods_present', $Pages, $Limit);

            if(!empty($ListGoods)){
                $Code = 1;
                $Msgs = '查询成功';
            }else{
                $Code = 2;
                $Msgs = '暂无商品';
            }

            $this->ShowAjax($Code, $Msgs, $ListGoods);
        }
    }

    # 商品详情
    public function mallDetail(){
        if(request()->isPost()){
            $Goods = input('post.goods_id','','intval');

            if(empty($Goods)){
                $this->ShowAjax(2, '缺少goods_id参数');
            }

            $MallGoods = new MallGoodsModel;

            $FindGoods = $MallGoods->mList(['goods_id'=>$Goods], 'goods_id,goods_class,goods_name,goods_img,goods_primary,goods_present,goods_detail,goods_sale');

            if(!empty($FindGoods)){
                $Code = 1;
                $Msgs = '查询成功';
            }else{
                $Code = 2;
                $Msgs = '查询失败';
            }

            $this->ShowAjax($Code, $Msgs, $FindGoods);
        }
    }

}